//
//  MYMachineGoodListViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/5/14.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYMachineGoodListViewController.h"
#import "MY_getVendingListHttpApi.h"
#import "MyClassificationCell.h"
#import "MYMachineViewController.h"
#import "MYGoodDetailView.h"
#import "MYGoodDetailCell.h"

@interface MYMachineGoodListViewController ()
{
    NSInteger selectindex;
}
@property (nonatomic,copy) NSString   *strMachineId;
@property (nonatomic,copy) NSString   *strControlNumber;
@property (nonatomic,copy) NSString   *strclassificationId;
@property (nonatomic,strong) NSMutableArray *arrayclassification;
@property (nonatomic,strong) NSMutableArray *arrayGoods;
@property (nonatomic,weak) IBOutlet   UILabel     *labHead;
@property (nonatomic,weak) IBOutlet   UITableView *tabMain;
@property (nonatomic,weak) IBOutlet   UITableView *tabDate;
@property (nonatomic,weak) IBOutlet   UIScrollView *scrollMain;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;

@end

@implementation MYMachineGoodListViewController

-(instancetype)initWithMachineId:(NSString *)sid ControlNumber:(NSString *)controlNumber
{
    self = [super initWithNibName:@"MYMachineGoodListViewController" bundle:nil];
    if (self) {
        self.strMachineId = sid;
        self.strControlNumber = controlNumber;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=11.0) {
        
    }else{
        // 针对 11.0 以下的iOS系统进行处理
        for (NSLayoutConstraint *constraint in self.viewHead.superview.constraints) {
            if (constraint.firstItem == self.viewHead && constraint.firstAttribute == NSLayoutAttributeTop) {
                constraint.constant = 20;
            }
        }
        
    }
    selectindex = 0;
    NSString *title = [NSString stringWithFormat:@"工控号:%@",self.strControlNumber];
    [self.labHead setText:title];
    [self getClassificationDataFromServices];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getGoodListFromServices
{
    [MY_getVendingListHttpApi mygoodsListWithMachineDetailWithID:self.strMachineId ClassificationId:self.strclassificationId PageNum:@"0" PageSize:@"100" complationHandler:^(OCJBaseResponceModel *responseModel) {
        [self.arrayGoods removeAllObjects];
        NSArray *good = [responseModel.ocjDic_data objectForKey:@"records"];
        [self.arrayGoods addObjectsFromArray:good];
//        [self setGoodsDataToView];
        [self.tabDate reloadData];
    }];
}

- (void)getClassificationDataFromServices
{
    [MY_getVendingListHttpApi myGetGoodclassificationWithcomplationHandler:^(OCJBaseResponceModel *responseModel) {
        NSArray *array = [responseModel.ocjDic_data objectForKey:@"array"];
        [self.arrayclassification removeAllObjects];
        [self.arrayclassification addObjectsFromArray:array];
        [self.tabMain reloadData];
        
        NSDictionary *dic = [self.arrayclassification objectAtIndex:0];
        NSString *strid = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
        self.strclassificationId = strid;
        [self getGoodListFromServices];
        
    }];
}

- (void)setGoodsDataToView
{
    for (UIView *views in self.scrollMain.subviews) {
        [views removeFromSuperview];
    }
    
    CGFloat viewWidth = WINDOWWIDTH*130/375;
    CGFloat viewhigh = viewWidth*154/130;
    
    for (int i = 0; i < [self.arrayGoods count]; i++) {
        NSDictionary *dic = [self.arrayGoods objectAtIndex:i];
        NSInteger line = i/2;
        NSInteger column = i%2;
        CGFloat pointx = WINDOWWIDTH*14/375 + (viewWidth + WINDOWWIDTH*17/375)*column;
        CGFloat pointy = 15 + viewhigh*line;
        MYGoodDetailView *view = [[MYGoodDetailView alloc] initWithFrame:CGRectMake(pointx, pointy, viewWidth, viewhigh)];
        [view setBackgroundColor:[UIColor clearColor]];
        NSString *imgurl = [NSString stringWithFormat:@"%@",[dic objectForKey:@"picture"]];
        if ([imgurl stringIsValid]) {
            [view.imgIcon sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"deafultgood"]];
        }
        else{
            [view.imgIcon setImage:[UIImage imageNamed:@"deafultgood"]];
        }
        
        NSString *name = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
        if ([name stringIsValid]) {
            [view.labTitle setText:name];
        }
        else
        {
            [view.labTitle setText:@"康师傅茉莉蜜茶"];
        }
        
        
        NSString *sold = [NSString stringWithFormat:@"%@",[dic objectForKey:@"sold"]];//售卖
        NSString *inventory = [NSString stringWithFormat:@"%@",[dic objectForKey:@"inventory"]];//库存
        [view.labStock setText:[NSString stringWithFormat:@"货存%@",inventory]];
        [view.labRestocking setText:[NSString stringWithFormat:@"待补%@",sold]];

        
        [self.scrollMain addSubview:view];
    }
    NSInteger count = [self.arrayGoods count];
    CGFloat highnum = count/2;
    if (count%2 != 0) {
        highnum = highnum + 1;
    }
    
    CGFloat high = 15 + viewhigh*highnum;
    CGFloat scrollhigh = self.scrollMain.frame.size.height;
    if (high < scrollhigh || high == scrollhigh) {
        high = scrollhigh + 1;
    }
    
    [self.scrollMain setContentSize:CGSizeMake(self.scrollMain.frame.size.width, high)];
}

- (IBAction)onBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onDetailButtonPressed:(id)sender
{
    MYMachineViewController *ctrl = [[MYMachineViewController alloc] initWithMachineId:self.strMachineId];
    [self.navigationController pushViewController:ctrl animated:YES];

}

#pragma tableViewDelegate


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num = 0;
    if (tableView.tag == 101) {
        num = [self.arrayclassification count];
    }
    else
    {
        num = [self.arrayGoods count];
    }
    return num;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat height = 50;
    if (tableView.tag == 102) {
        height = 65;
    }
    
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        MyClassificationCell *cell  = (MyClassificationCell*)[tableView dequeueReusableCellWithIdentifier:@"MyClassificationCell"];
        
        if (!cell)
        {
            [tableView registerNib:[UINib nibWithNibName:@"MyClassificationCell" bundle:nil] forCellReuseIdentifier:@"MyClassificationCell"];
            cell = [tableView dequeueReusableCellWithIdentifier:@"MyClassificationCell"];
        }
        NSDictionary *dic = [self.arrayclassification objectAtIndex:indexPath.row];
        [cell setCellTextWithString:[dic objectForKey:@"name"]];
        
        if (selectindex == indexPath.row) {
            [cell setCellSelected:YES];
        }
        else
        {
            [cell setCellSelected:NO];
        }
        
        return cell;
    }
    else
    {
        MYGoodDetailCell *cell  = (MYGoodDetailCell*)[tableView dequeueReusableCellWithIdentifier:@"MYGoodDetailCell"];
        
        if (!cell)
        {
            [tableView registerNib:[UINib nibWithNibName:@"MYGoodDetailCell" bundle:nil] forCellReuseIdentifier:@"MYGoodDetailCell"];
            cell = [tableView dequeueReusableCellWithIdentifier:@"MYGoodDetailCell"];
        }
        NSDictionary *dic = [self.arrayGoods objectAtIndex:indexPath.row];
        [cell setDataToViewWithDictionary:dic];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        NSDictionary *dic = [self.arrayclassification objectAtIndex:indexPath.row];
        NSString *strid = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
        self.strclassificationId = strid;
        [self getGoodListFromServices];
        selectindex = indexPath.row;
        [_tabMain reloadData];
    }
    
}



-(NSMutableArray *)arrayclassification
{
    if (_arrayclassification == nil) {
        _arrayclassification = [[NSMutableArray alloc] init];
    }
    return _arrayclassification;
}

- (NSMutableArray *)arrayGoods
{
    if (_arrayGoods == nil) {
        _arrayGoods = [[NSMutableArray alloc] init];
    }
    return _arrayGoods;
}


@end
